package com.cy.ppj.web.admin.user;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.JsonObjectMapper;
import com.cy.ppj.dao.mongodb.BrokerCommissionSettingsLogDao;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.admin.BrokerCommissionSettingsAO;
import com.cy.ppj.model.doc.BrokerCommissionSettingsLog;
import com.cy.ppj.model.domian.admin.SystemSettings;
import com.cy.ppj.model.dto.sys.BrokerCommissionSettingsDTO;
import com.cy.ppj.model.enums.account.BrokerLevel;
import com.cy.ppj.model.vo.admin.setting.BrokerCommissionSettingsVO;
import com.cy.ppj.service.sys.SystemSettingsService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 团队返佣设置
 * @author: Ocean
 * @create: 2018-09-05 14:52
 **/
@Slf4j
@RestController
@RequestMapping("/admin/broker/commission")
@Api(tags = "后台-经纪人设置")
public class AdminBrokerCommissionController extends BaseController {

    @Autowired
    private SystemSettingsService systemSettingsService;
    @Autowired
    private BrokerCommissionSettingsLogDao brokerCommissionSettingsLogDao;
    

    @ApiOperation("经纪人设置")
    @GetMapping("/params")
    public Result<List<BrokerCommissionSettingsVO>> params() {
        BrokerCommissionSettingsDTO brokerCommissionSettings = systemSettingsService.queryBrokerCommissionSettingsInCache();
        List<BrokerCommissionSettingsVO> list = new ArrayList<>();
        // 正式经纪人
        BrokerCommissionSettingsVO offical = new BrokerCommissionSettingsVO();
        offical.setId(brokerCommissionSettings.getOfficalId());
        offical.setBrokerLevel(BrokerLevel.OFFICIAL);
        offical.setUpgradeCondition(brokerCommissionSettings.getOfficalUpgradeCondition());
        offical.setKeepCondition(brokerCommissionSettings.getOfficalKeepCondition());
        offical.setRate(brokerCommissionSettings.getOfficalRate());
        offical.setUpdateTime(brokerCommissionSettings.getOfficalUpdateTime());
        list.add(offical);
        
        // 金牌经纪人
        BrokerCommissionSettingsVO golden = new BrokerCommissionSettingsVO();
        golden.setId(brokerCommissionSettings.getGoldenId());
        golden.setBrokerLevel(BrokerLevel.GOLDEN);
        golden.setUpgradeCondition(brokerCommissionSettings.getGoldenUpgradeCondition());
        golden.setKeepCondition(brokerCommissionSettings.getGoldenKeepCondition());
        golden.setRate(brokerCommissionSettings.getGoldenRate());
        golden.setUpdateTime(brokerCommissionSettings.getGoldenUpdateTime());
        list.add(golden);
        
        // 金牌经纪人
        BrokerCommissionSettingsVO diamond = new BrokerCommissionSettingsVO();
        diamond.setId(brokerCommissionSettings.getDiamondId());
        diamond.setBrokerLevel(BrokerLevel.DIAMOND);
        diamond.setUpgradeCondition(brokerCommissionSettings.getDiamondUpgradeCondition());
        diamond.setKeepCondition(brokerCommissionSettings.getDiamondKeepCondition());
        diamond.setRate(brokerCommissionSettings.getDiamondRate());
        diamond.setUpdateTime(brokerCommissionSettings.getDiamondUpdateTime());
        list.add(diamond);
        
        
        return ok(null, list);
    }

    @ApiOperation("经纪人设置编辑")
    @PostMapping("/edit")
    public Result edit(@Validated BrokerCommissionSettingsAO brokerCommissionSettings, BindingResult bindingResult) {

        this.checkError(bindingResult);
        Date time = new Date();
        BrokerLevel brokerLevel = null;
        BigDecimal oldUpgradeCondition = BigDecimal.ZERO;
        BigDecimal oldKeepCondition = BigDecimal.ZERO;
        BigDecimal oldRate = BigDecimal.ZERO;
        BrokerCommissionSettingsDTO brokerCommissionSettingsDTO = systemSettingsService.queryBrokerCommissionSettingsInCache();
        if(brokerCommissionSettingsDTO.getOfficalId().equals(brokerCommissionSettings.getId())) {
        	// 正式经纪人
        	brokerLevel = BrokerLevel.OFFICIAL;
        	oldUpgradeCondition = brokerCommissionSettingsDTO.getOfficalUpgradeCondition();
        	oldKeepCondition = brokerCommissionSettingsDTO.getOfficalKeepCondition();
        	oldRate = brokerCommissionSettingsDTO.getOfficalRate();
        }else if (brokerCommissionSettingsDTO.getGoldenId().equals(brokerCommissionSettings.getId())) {
        	// 金牌经纪人
        	brokerLevel = BrokerLevel.GOLDEN;
        	oldUpgradeCondition = brokerCommissionSettingsDTO.getGoldenUpgradeCondition();
        	oldKeepCondition = brokerCommissionSettingsDTO.getGoldenKeepCondition();
        	oldRate = brokerCommissionSettingsDTO.getGoldenRate();
        }else if(brokerCommissionSettingsDTO.getDiamondId().equals(brokerCommissionSettings.getId())) {
        	// 钻石经纪人
        	brokerLevel = BrokerLevel.DIAMOND;
        	oldUpgradeCondition = brokerCommissionSettingsDTO.getDiamondUpgradeCondition();
        	oldKeepCondition = brokerCommissionSettingsDTO.getDiamondKeepCondition();
        	oldRate = brokerCommissionSettingsDTO.getDiamondRate();
        }
        
        if(brokerLevel == null) {
        	return fail("数据异常");
        }
        
        SystemSettings systemSettings = new SystemSettings();
        systemSettings.setId(brokerCommissionSettings.getId());

        brokerCommissionSettings.setId(null);
        String json = JsonObjectMapper.toJson(brokerCommissionSettings);

        systemSettings.setValue(json);
        systemSettings.setUpdateTime(time);
        systemSettingsService.updateSystemSettings(systemSettings);

        BrokerCommissionSettingsLog brokerCommissionSettingsLog = new BrokerCommissionSettingsLog();
        brokerCommissionSettingsLog.setBrokerLevel(brokerLevel);
        brokerCommissionSettingsLog.setOldUpgradeCondition(oldUpgradeCondition);
        brokerCommissionSettingsLog.setNewUpgradeCondition(brokerCommissionSettings.getUpgradeCondition());
        brokerCommissionSettingsLog.setOldKeepCondition(oldKeepCondition);
        brokerCommissionSettingsLog.setNewKeepCondition(brokerCommissionSettings.getKeepCondition());
        brokerCommissionSettingsLog.setOldRate(oldRate);
        brokerCommissionSettingsLog.setNewRate(brokerCommissionSettings.getRate());
        brokerCommissionSettingsLog.setCreateTime(time);
        brokerCommissionSettingsLogDao.insert(brokerCommissionSettingsLog);
        
        return ok();
    }
    
    @ApiOperation("修改记录")
    @GetMapping("/edit/records")
    public PageBean<BrokerCommissionSettingsLog> editRecords(int page, int limit) {
    	if(CommonUtils.isEmpty(page)) {
    		page = 1;
    	}
    	if(CommonUtils.isEmpty(limit)) {
    		limit = 10;
    	}
    	page = page - 1;
    	Pageable pageable = new PageRequest(page, limit, Sort.Direction.DESC, "createTime");
    	Page<BrokerCommissionSettingsLog> pageModel = brokerCommissionSettingsLogDao.findAll(pageable);
    	List<BrokerCommissionSettingsLog> list = pageModel.getContent();
    	int total = (int) pageModel.getTotalElements();
    	return new PageBean<>(list, total);
    }
    
}
